<template>
    <div>
        <div class="container">
            <el-main>
                <el-form ref="memberGradeFormRef" :rules="memberGradeFormRules" :model="memberGradeForm" :label-width="formLabelWidth">
                    <el-form-item label="ID" prop="id" v-if="false">
                        <el-input v-model="memberGradeForm.id" c/>
                    </el-form-item>
					<!-- 常规信息 -->
					<el-form-item label="等级名称" prop="gradeName">
                        <el-input v-model="memberGradeForm.gradeName" clearable class="handle-input mr10" />
                    </el-form-item>
					<el-form-item label="最大值" prop="upMax">
                        <el-input v-model="memberGradeForm.upMax" clearable class="handle-input mr10" />
                    </el-form-item>
					<el-form-item label="最小值" prop="upMin">
                        <el-input v-model="memberGradeForm.upMin" clearable class="handle-input mr10" />
                    </el-form-item>
					<el-form-item label="晋升方式" prop="upWay">
                        <el-input v-model="memberGradeForm.upWay" clearable class="handle-input mr10" v-if="false"/>
                        <el-select v-model="memberGradeForm.upWay" class="handle-select mr10" placeholder="请选择">
                            <el-option v-for="item in memberGradeUpWayOpt" :key="item.value" :label="item.label" :value="item.value" />
                        </el-select>
                    </el-form-item>
                </el-form>
            </el-main>
			<el-footer>
				<el-button type="primary" @click="func_onSubmit" plain>保存数据</el-button>
				<el-button type="success" @click="func_onReset" plain>重置表单</el-button>
				<el-button @click="func_closeDialog" plain>取 消</el-button>
			</el-footer>
        </div>
    </div>
</template>

<script>
import { onMounted, ref, reactive, toRefs, watch } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { ElMessage, ElMessageBox } from "element-plus";
import { findMemberGradeInfoById, addMemberGrade, saveMemberGrade} from '../../../../api/hairmanager/memberGrade';
import { loadMemberGradeUpWay, } from "../../../../api/dict";

import { NO_DATA_SELECT_ERROR, SUCCESS_ADDDATA_DIALOG_MESSAGE, SUCCESS_DELDATA_DIALOG_MESSAGE, SUCCESS_EDITDATA_DIALOG_MESSAGE, formLabelWidth, } from "../../../../api/CommonErrorMessage.js";

export default {
    name: "memberGradeInfo",
	props:{
		selectValue: Object
	},
	emits: ["memberGradeData"],
    setup(props, cex){
		onMounted(() => {
		});
        const router = useRouter();
		const route = useRoute();
        const formLabelWidth = ref("170px");
        const loadFlg = ref(false);
		/**
         * @name 定义业务表单
         */
        const getInitFormData = () => {
            return {
				gradeName: '',
				id: 0,
				upMax: 0,
				upMin: 0,
				upWay: '',
			}
        };
		const memberGradeUpWayOpt = ref([]);
        /**
         * @name 表单初始化
         */
        const resetMemberGradeForm = () => {
            Object.assign(memberGradeForm, getInitFormData());
            
        };
		const memberGradeForm = reactive(getInitFormData());
		const memberGradeFormRef = ref(null);
        const memberGradeFormRules = {
            gradeName: [
                { required: true, message: "请输入等级名称", trigger: "blur" },
            ],
            upWay: [
                { required: true, message: "请选择等级晋升方式", trigger: "blur" },
            ],
        };
		// =========================================================================================
		// 主业务处理 ========================================================================================= START
        // 提交
        const func_onSubmit = () => {
            // 表单校验
            memberGradeFormRef.value.validate((valid) => {
                if (valid) {
                    if(loadFlg.value){
                        saveMemberGrade(memberGradeForm).then((res) => {
							if(res.code != 200) {
								ElMessage.error(res.message);
							} else {
								resetMemberGradeForm();
								ElMessage.success(SUCCESS_EDITDATA_DIALOG_MESSAGE);
								cex.emit('memberGradeData', {state: 1, data: []});
							}
						});
                    } else {
                        addMemberGrade(memberGradeForm).then((res) => {
							if(res.code != 200) {
								ElMessage.error(res.message);
							} else {
								resetMemberGradeForm();
								ElMessage.success(SUCCESS_ADDDATA_DIALOG_MESSAGE);
								cex.emit('memberGradeData', {state: 1, data: []});
							}
						});
                    }
                } else {
                    return false;
                }
            });
        };
        // 重置
        const func_onReset = () => {
			resetMemberGradeForm();
        };
		const func_closeDialog = () => {
            cex.emit('memberGradeData', {state: 1, data: []});
        };
        // 页面初始化
        const func_onload = (v) => {
			if(loadFlg.value){
				findMemberGradeInfoById(v).then((res) => {
					Object.keys(memberGradeForm).forEach((item) => {
                        memberGradeForm[item] = res.data[item];
                    });
				});
			} else {
				resetMemberGradeForm();
			}
			memberGradeUpWayOpt.value = [];
			loadMemberGradeUpWay().then((res) => {
				res.data.forEach((item) => {
					memberGradeUpWayOpt.value.push({label: item['label'],value: item['value']});
				});
			});
        }
		// 主业务处理 ========================================================================================= END
        watch(() => props.selectValue, (val) => {
            let id = val['id'];
            loadFlg.value = val['load'];
			func_onload(id);
        }, {deep: true,immediate: true});
        return {
            // 系统变量 =========================================================================================
			loadFlg, formLabelWidth, memberGradeUpWayOpt, memberGradeFormRules, memberGradeFormRef, memberGradeForm,
            // 主业务调用方法 =========================================================================================
			resetMemberGradeForm, getInitFormData, func_onload, func_onSubmit, func_onReset, func_closeDialog,
        };
    },
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 300px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
</style>